<?php
namespace app\wechat\controller;

use think\View;

class Team extends Common
{
    public function index()
    {
        // 获取合伙人列表数据
        $param = [
            'member_id' => session('member_id')
        ];
        $data = call_interface('partner_team', $param);
        //dump($data);
        $levels = ['', '省', '市', '区'];
        $this->assign('data', [
            'partners' => $data['data']['partners'],
            'level' => $levels[$data['data']['level']],
            'region' => $data['data']['region'][$data['data']['level']-1],
            'member_id' => session('member_id')
        ]);

        // 获取下级合伙人团队
        $junior_data = call_interface('subordinate_partner', $param);
        //dump($junior_data);
        if ($junior_data['code'] == 200) {
            $this->assign('junior_data', $junior_data['data']);
        }
        return view();
    }

    // 合伙人详情
    public function partner_info() {
        if (!input('?param.id')) {
            return view('public/error');
        }
        $id = input('param.id');
        // 获取合伙人数据
        $param = [
            'id' => $id,
            'member_id' => session('member_id')
        ];
        $data = call_interface('get_partner_info', $param);
        //dump($data);
        $this->assign('data', $data['data']);

        // 获取管理区域
        $param = [
            'partner_id' => $id
        ];
        $manage_area = call_interface('get_allocation_area', $param);
        $this->assign('manage_area', $manage_area['data']);

        $this->assign('id', $id);
        return view();
    }

    // 维修店团队汇总
    public function repair_team() {
        // 获取已审核维修店数据
        $partner_id = db('partner')->where('member_id', session('member_id'))->value('id');
        $param = [
            'partner_id' => $partner_id,
            'model' => 1
        ];
        $data = call_interface('manage_collect', $param);
        $this->assign('data', $data['data']);

        // 获取待审核维修店数据
        $param = [
            'member_id' => session('member_id'),
            'model' => 1
        ];
        $data = call_interface('manage_team_check_pending_data', $param);
        $this->assign('repair_num', $data['data']['num']);
        return view();
    }

    // 维修店团队列表
    public function repair_team_list() {
        $region_id = input('param.region_id');
        // 获取已审核维修店数据
        $param = [
            'region_id' => $region_id,
            'member_id' => session('member_id'),
            'model' => 1,
        ];
        $data = call_interface('manage_team', $param);
        $this->assign('data', $data['data']);
        return view();
    }

    // 维修店待审核列表
    public function repair_list() {
        if (request()->isAjax()) {
            $audit_status = input('param.status');
            // 获取维修店数据
            $param = [
                'member_id' => session('member_id'),
                'audit_status' => $audit_status,
                'model' => 1,
            ];
            $data = call_interface('manage_team_check_pending_data', $param);

            if ($data['code'] == 200) {
                $view = new View();
                $view->assign('data', $data['data']['list']);
                $tpl = $view->fetch('team/repair_list_tpl');
                $data['tpl'] = $tpl;
            }
            return json($data);
        }
        return view();
    }

    // 维修店详情
    public function repair_info() {
        // 获取维修店数据
        $param = [
            'repair_id' => input('param.id', 0)
        ];

        $data = call_interface('get_repair_info', $param);
        //dump($data);
        $this->assign('data', $data['data']);
        return view();
    }

    // 经销商负责区域数据统计
    public function dealer_team() {
        $partner_id = db('partner')->where('member_id', session('member_id'))->value('id');
        $param = [
            'partner_id' => $partner_id,
            'model' => 2
        ];
        $data = call_interface('manage_collect', $param);
        //print_r($data);
        $this->assign('data', $data['data']);

        // 获取待审核维修店数据
        $param = [
            'member_id' => session('member_id'),
            'model' => 2
        ];
        $data = call_interface('manage_team_check_pending_data', $param);
        $this->assign('dealer_num', $data['data']['num']);
        return view();
    }

    // 经销商团队列表
    public function dealer_team_list() {
        $region_id = input('param.region_id');
        // 获取已审核维修店数据
        $param = [
            'region_id' => $region_id,
            'member_id' => session('member_id'),
            'model' => 2,
        ];
        $data = call_interface('manage_team', $param);
        //dump($data);
        $this->assign('data', $data['data']);
        return view();
    }

    // 待审核列表
    public function dealer_list() {
        if (request()->isAjax()) {
            $audit_status = input('param.status');
            // 获取经销商数据
            $param = [
                'member_id' => session('member_id'),
                'audit_status' => $audit_status,
                'model' => 2,
            ];
            $data = call_interface('manage_team_check_pending_data', $param);

            if ($data['code'] == 200) {
                $view = new View();
                $view->assign('data', $data['data']['list']);
                $tpl = $view->fetch('team/dealer_list_tpl');
                $data['tpl'] = $tpl;
            }
            return json($data);
        }
        return view();
    }

    // 经销商详情
    public function dealer_info() {
        // 获取维修店数据
        $param = [
            'dealer_id' => input('param.id', 0)
        ];

        $data = call_interface('get_dealer_info', $param);
        //dump($data);
        $this->assign('data', $data['data']);
        return view();
    }

    // 搜索
    public function search_shop() {
        $type = input('param.type');
        if (input('?param.keyword')) {
            // 获取维修店数据
            $param = [
                'member_id' => session('member_id'),
                'keyword' => input('param.keyword'),
                'model' => $type
            ];

            $data = call_interface('search_team', $param);
            $data = $data['data'];

            $this->assign('data', $data);
        }
        $this->assign('type', $type);
        return view();
    }

    // 审核
    public function audit() {
        // 获取维修店数据
        $param = [
            'obj_id' => input('param.obj_id', 0),
            'model' => input('param.model', ''),
            'member_id' => session('member_id'),
            'audit_status' => input('param.audit_status', ''),
            'msg' => input('param.msg', '')
        ];

        $data = call_interface('audit', $param);
        return json($data);
    }

    // 审核进度
    public function audit_schedule() {
        // 获取维修店数据
        $param = [
            'obj_id' => input('param.obj_id', 0),
            'model' => input('param.model', ''),
        ];
        $data = call_interface('audit_schedule', $param);
        $this->assign('data', $data['data']);
        return view();
    }

    // 区域分配
    public function allocation_area() {
        if (request()->isPost()) {
            $ids = input('param.region_id/a');
            $ids = $ids ? implode(',', $ids) : '0';
            $param = [
                'ids' => $ids,
                'partner_id' => input('param.partner_id')
            ];
            //dump($param);
            $data = call_interface('allocation_area', $param);
            return json($data);
        }else {
            $id = input('param.id');
            $partner = db('partner')->alias('p')->field('p.province, p.city, p.area, da.level')
                ->join('mob_duty_area da', 'da.member_id = p.member_id and da.member_type = 3')
                ->where('p.id', $id)->find();
            $region = ['', 'province', 'city', 'area', 'street'];
            $region_id = $partner[$region[$partner['level']]];
            // 获取下级地址
            $regions = db('region')->alias('r')->field('r.*, ma.id as maid')
                ->join('mob_manage_area ma', 'ma.region_id = r.region_id and ma.status = 1', 'LEFT')
                ->where('r.parent_id', $region_id)->select();
            //dump($regions);
            $this->assign('data', $regions);
            $this->assign('id', $id);

            // 获取已有的管理区域
            $param = [
                'partner_id' => $id
            ];
            $old_area = call_interface('get_allocation_area', $param);
            //dump($old_area);
            $old_region_ids = [];
            foreach ($old_area['data'] as $v) {
                $old_region_ids[] = $v['region_id'];
            }
            $this->assign('old_area', $old_region_ids);
            return view();
        }
    }
}
